<script>
// #ifdef APP-PLUS
import APPUpdate from '@/uni_modules/zhouWei-APPUpdate/js_sdk/appUpdate';
// #endif
import config from './common/config.js';
import httpLbApi from './common/httpLbApi.js';
export default {
	data() {
		return {
			system: '', //系统
			downUrl: '', //下载url
			version: '', //版本号
		}
	},
	onLoad() {

	},
	onLaunch: function () {

		// ////console.log('App Launch');
		// #ifdef APP-PLUS
		APPUpdate();
		// #endif
		//首页弹窗状态
		// uni.setStorageSync('shouye', 1);
		// 神龙召唤说明弹窗
		uni.setStorageSync('zhaohuanshuoming', 1);
		// 有限池说明弹窗
		uni.setStorageSync('yxcshuoming', 1);
		// 充值弹窗
		// uni.setStorageSync('czsm', 1);
	},
	onShow: function () {
		this.userList()
		// this.downSvga()
		uni.hideTabBar();
		//#ifdef APP-PLUS
		this.appUp()
		//#endif
		//#ifdef MP-WEIXIN
		////console.log(uni.getAccountInfoSync().miniProgram.appId, 'appid');
		if (uni.getAccountInfoSync().miniProgram.appId == 'wx7000c07ad1b0e391') {
			uni.setStorageSync('wechat_type', '1')
		}
		// //#endif
		// // #ifdef APP-PLUS
		// let equipment = uni.getDeviceInfo('deviceBrand')
		// ////console.log('设备名称:', equipment.brand)

		// #endif

		// #ifdef APP-PLUS
		setTimeout(() => {
			//获取第三方传来的参数
			let args = plus.runtime.arguments;
			if (args) {
				//对传来的参数做处理并跳转
				let arg = args.split(':/');
				var vars = args.split('&');
				for (var i = 0; i < vars.length; i++) {
					var pair = vars[i].split('=');
					if (pair[0] == 'invite_code') {
						uni.setStorageSync('invite_code', pair[1]);
					} else if (pair[0] == 'idef') {
						uni.setStorageSync('idef', pair[1]);
					}
				}
				uni.navigateTo({
					url: arg[1]
				});
				plus.runtime.arguments = '';
				// ////console.log(args)
				// 处理args参数，如直达到某新页面等
			}
		}, 0);
		clearTimeout();
		// #endif
	},
	onHide: function () {
		////console.log('App Hide');
	},
	methods: {
		userList() {
			this.$Request.get(this.$api.user.index).then(res => {
				if (res.code == 406) {
					return;
				}
				console.log('res',res)
				uni.setStorageSync('username', res.data.userInfo.nickname)
			});
		},
		// downSvga() {
		// 	console.log("下载")
		// 	let url = "https://new.qingfentool.vip/app/animation_2.svga"
		// 	// 下载svga存储到本地
		// 	uni.downloadFile({
		// 		url: url,
		// 		success: (val) => {
		// 			if (val.statusCode  === 200) {
		// 				console.log("下载成功",val)
		// 				uni.setStorageSync('svga',val.tempFilePath)
		// 			}
		// 		},
		// 		fail: (err) => {
		// 			console.log("下载失败",err)
		// 		}
		// 	})
		// },
		appUp() {
			let that = this
			uni.getSystemInfo({ //获取系统信息
				success: (res) => { 
					that.system = res.platform
					if (that.system == "android") { //检测当前平台，如果是安卓则启动安卓更新
						plus.runtime.getProperty(plus.runtime.appid, function (wgtinfo) {
							////console.log(wgtinfo) //获取配置信息
							that.version = wgtinfo.version //当前版本号
							that.AndroidCheckUpdate();
						});
					} else if (that.system == 'ios') {
						that.iosMeth()
					} else {
						////console.log('其他系统')
					}
				}
			})
		},
		AndroidCheckUpdate() {
			var _this = this;
			let data = { //传参：type：当前手机类型；version：当前版本号
				type: _this.system,
				version: _this.version
			}
			_this.$Request.get(_this.$api.index.versionCode, data).then(res => {
				// ////console.log(res, "新包接口")
				var isUpdata = res.data.status
				_this.downUrl = res.data.url
				// ////console.log(downUrl)
				if (isUpdata == 1) { //为1的时候进行更新
					uni.showModal({
						title: "发现新版本",
						content: res.data.content,
						showCancel: false,
						confirmText: '更新',
						confirmColor: '#F54E40',
						success: (confirm) => {
							if (confirm.confirm) {
								////console.log("apk下载的地址", _this.downUrl)
								var downloadApkUrl = res.data.url; //apk下载的地址

								var dtask = plus.downloader.createDownload(downloadApkUrl, {},
									function (d, status) { //新建下载任务
										if (status == 200) {
											plus.runtime.install(plus.io
												.convertLocalFileSystemURL, (d
													.filename), {}, {},
												function (error) {
													uni.showToast({
														title: '安装失败',
														duration: 1500,
														icon: 'none'
													});
												})
										} else {
											uni.showToast({
												title: '更新失败',
												duration: 1500,
												icon: 'none'
											});
										}
									})
								dtask.start();
								var prg = 0;
								var showLoading = plus.nativeUI.showWaiting("正在下载");
								dtask.addEventListener('statechanged', function (task, status) {

									////console.log(task)
									// 给下载任务设置一个监听 并根据状态 做操作
									switch (task.state) {
										case 1:
											showLoading.setTitle("正在下载");
											break;
										case 2:
											showLoading.setTitle("已连接到服务器");
											break;
										case 3:
											prg = parseInt( //下载的进度
												(parseFloat(task.downloadedSize) /
													parseFloat(task.totalSize)) * 100
											);
											showLoading.setTitle("正在更新" + prg + "% ");
											break;
										case 4:
											plus.nativeUI.closeWaiting(); //关闭系统提示框
											if (status == 200) {
												uni.showModal({
													title: "",
													content: "更新成功，确定现在安装吗",
													confirmText: '安装',
													confirmColor: '#EE8F57',
													success: function (q) {
														if (q.confirm) {
															plus.runtime
																.install(task
																	.filename, {
																	force: true
																},
																	function (res) {
																		utils
																			.showToast(
																				'更新成功，重启中'
																			);
																		plus.runtime
																			.restart();
																	})
														} else {
															plus.runtime
																.quit(); // 退出应用
														}
													}
												})
											}
											//下载完成
											break;
									}

								})
							} else {

							}
						}
					})

				}

			})
		},
		iosMeth() {
			plus.runtime.getProperty(plus.runtime.appid, (info) => {
				//版本号
				uni.getSystemInfo({ //获取手机系统
					success: (res) => {
						this.system = res.platform
					}
				})
				let version = info.version;
				let data = { //传参：type：当前手机类型；version：当前版本号
					type: this.system,
					version
				}
				this.$Request.get(this.$api.index.versionCode, data).then(res => {
					this.downUrl = res.data.url
					if (res.data.status == 1) {
						uni.showModal({
							title: '发现新版本',
							content: res.data.content,
							showCancel: false,
							confirmText: '更新',
							confirmColor: '#F54E40',
							success: (res) => {
								if (res.confirm) {
									plus.runtime.openURL(this.downUrl)
								}
							}
						})
					}
				})
			})
		}
	}

};
</script>

<style lang="scss">
//动画
@import 'animate.css';
// @import url('https://pd.pdaxiang.com/upload/static/jianti.css');
/*每个页面公共css */
@import 'uview-ui/index.scss';

@font-face {
	font-family: 'YouSheBiaoTiHei';
	src: url('font/YouSheBiaoTiHei-2.ttf'); //优设标题黑
}

@font-face {
	font-family: 'Alimama_DongFangDaKai_Regular';
	src: url('font/Alimama_DongFangDaKai_Regular.ttf'); //宋体
}

@font-face {
	font-family: 'ZhanKuGaoDuanHei';
	src: url('font/zcool-gdh_Regular.ttf'); //站酷高端黑
}

page {
	minheight: 100%
}

.flexs {
	display: flex;
	justify-content: center;
	align-items: center;
}

.flexs1 {
	display: flex;
	justify-content: flex-start;
	align-items: center;
}

.allbtn {
	background: #FFFFFF;
}

.emptya {
	padding-top: 220rpx;

	image {
		width: 390rpx;
		height: 390rpx;
	}

	text {
		margin-top: 10rpx;
	}
}

.color-f {
	color: #fff;
}

.color-h {
	color: #000;
}

.flex {
	display: flex;
}

.flex-w {
	display: flex;
	flex-wrap: wrap;
}

.flex-c-c {
	display: flex;
	align-items: center;
	justify-content: center;
}

.flex-a-c {
	display: flex;
	align-items: center;
}

.flex-a-s {
	display: flex;
	align-items: flex-start;
}

.flex-a-e {
	display: flex;
	align-items: flex-end;
}

.flex-j-c {
	display: flex;
	justify-content: center;
}

.flex-j-a {
	display: flex;
	justify-content: space-around;
}

.flex-j-b {
	display: flex;
	justify-content: space-between;
}

.flex-j-s {
	display: flex;
	justify-content: flex-start;
}

.flex-j-e {
	display: flex;
	justify-content: flex-end;
}

.flex-column {
	display: flex;
	flex-direction: column;
}

.of-t-w {
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.back-r-s {
	background-repeat: no-repeat !important;
	background-size: 100% 100% !important;
}

.font-Bold {
	font-weight: bold;
}

.size-10 {
	font-size: 10rpx;
}

.size-12 {
	font-size: 12rpx;
}

.size-14 {
	font-size: 14rpx;
}

.size-16 {
	font-size: 16rpx;
}

.size-18 {
	font-size: 18rpx;
}

.size-20 {
	font-size: 20rpx;
}

.size-22 {
	font-size: 22rpx;
}

.size-24 {
	font-size: 24rpx;
}

.size-26 {
	font-size: 26rpx;
}

.size-28 {
	font-size: 28rpx;
}

.size-30 {
	font-size: 30rpx;
}

.size-32 {
	font-size: 32rpx;
}

.size-34 {
	font-size: 34rpx;
}

.size-36 {
	font-size: 36rpx;
}

.size-38 {
	font-size: 38rpx;
}

.size-40 {
	font-size: 40rpx;
}

.size-42 {
	font-size: 42rpx;
}

.size-44 {
	font-size: 44rpx;
}

.size-46 {
	font-size: 46rpx;
}

.size-48 {
	font-size: 48rpx;
}

.size-50 {
	font-size: 50rpx;
}
</style>